// DocSecur.cpp : main source file for DocSecur.exe
//

#include "stdafx.h"
#include "stdftx.h"
#include "FileTools.h"


BOOL IsFileExist(wchar_t* filename)
{
	FILE* fp = wfopen(filename, L"rb");

	if( fp!=NULL )
	{
		fclose(fp);
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BOOL IsDirExist(wchar_t* path)
{
	BOOL ret = FALSE;

#ifndef __SYMBIAN32__	
	DWORD attr = GetFileAttributes(path);
	if( (attr!=-1) && (attr&FILE_ATTRIBUTE_DIRECTORY) )
	{
		ret = TRUE;
	}
#else
	RFs		fs;
	TEntry	entry;
	if( fs.Connect()==KErrNone )
	{
		TPtrC filename((TText16*)path);
		if( fs.Entry(filename,entry)==KErrNone )
		{
			if( entry.IsDir() )
			{
				ret = TRUE;
			}
		}
	}
#endif	

	return ret;
}

BOOL MakeFullPath(const wchar_t* path)
{
	wchar_t dir[260];

	wcscpy(dir,path);

	wchar_t* p = dir;

	for( ; *p; p++ )
	{
		if( p!=dir && *p=='\\' )
		{
			*p = 0;
			if( !IsDirExist(dir) )
			{
#ifdef __SYMBIAN32__
				wmkdir(dir,0755);
#else				
				wmkdir(dir);
#endif
			} 
			*p = '\\';
		}
	}

	if( !IsDirExist(dir) )
	{
#ifdef __SYMBIAN32__
		wmkdir(dir,0755);
#else				
		wmkdir(dir);
#endif
	} 

	return IsDirExist(dir);
}

BOOL HaveEnoughSpace(const wchar_t* path, unsigned int size)
{
#ifdef __SYMBIAN32__
	//check path
	TPtrC16 p( (const TText16*)path );
	TBuf16<10> buf;

	if( p.Length() < 3 )
	{
		return FALSE;
	}

	if( p.Mid(1,2).Compare( _L(":\\") ) != 0 )
	{
		return FALSE;
	}

	//get volume information
	TInt drive;
	RFs fs;
	TVolumeInfo vi;
	TInt	ret;

	RFs::CharToDrive( TChar(p[0]), drive );
	fs.Connect();
	ret=fs.Volume(vi,drive);
	fs.Close();

	if( ret != KErrNone )
	{
		return FALSE;
	}

	//
	TInt64 ulSize = size;
	if( vi.iFree > ulSize )
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}

#else
	ULARGE_INTEGER	ulFreeSpace={0};
	ULARGE_INTEGER	ulTotalSpace={0};

	//check path
	wchar_t disk_path[260];
	wcscpy(disk_path,path);
	int len = wcslen(disk_path);
	if( disk_path[len-1] != '\\' )
	{
		int i;
		for( i=len-1; i>=0; i-- )
		{
			if(disk_path[i]=='\\')
			{
				disk_path[i+1] = 0;
				break;
			}
		}
	}
	
	//get disk free space
	if( !GetDiskFreeSpaceEx( disk_path, &ulFreeSpace, &ulTotalSpace, NULL) )
	{
		return FALSE;
	}

	if( ulFreeSpace.HighPart > 0 || ulFreeSpace.LowPart > size )
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
#endif
}

int  GetFileSize(const wchar_t* filename)
{
	int	filesize = 0;
	FILE* fp = wfopen(filename, L"rb");
	if( fp==NULL )
	{
		return -1;
	}

	fseek(fp,0,SEEK_END);
	filesize = ftell(fp);
	fclose(fp);

	return filesize;
}

int GetDirSize(const wchar_t* directory,unsigned int* fileCount)
{
	unsigned int count = 0;
	TCHAR		path[MAX_PATH];
	CFindFile	finder;
	int			dirsize = 0;

	_tcscpy(path, directory);
	if (path[_tcslen(path) - 1] != _T('\\'))
		_tcscat(path, _T("\\"));
	_tcscat(path, _T("*.*"));

	if( !finder.FindFile(path) )
	{
		if(fileCount)
			*fileCount = count;
		return 0;
	}

    do
    {
        TCHAR    file[MAX_PATH];

		if( finder.IsDots() )
		{
			continue;
		}

		_tcscpy(file, directory);
		if (file[_tcslen(file) - 1] != _T('\\'))
			_tcscat(file, _T("\\"));
		_tcscat( file, finder.GetFileName() );

		DWORD attrib = GetFileAttributes(file);

        if( !(attrib&FILE_ATTRIBUTE_DIRECTORY) )
        {
			int size = GetFileSize(file);
			if( size != -1 )
			{
				dirsize += size;
				count++;
			}
        }
        else
        {
            dirsize += GetDirSize( file );
        }
    }while ( finder.FindNextFile() );

	if(fileCount)
		*fileCount = count;

    return dirsize;
}

BOOL RemoveDoc(wchar_t* name)
{
	if(IsDir(name))
	{
		return RemoveDirAll(name);
	}
	else
	{
		return DeleteFile(name);
	}
}

BOOL RemoveDirAll(wchar_t* pszDir)
{
	TCHAR		path[MAX_PATH];
	CFindFile	finder;

	_tcscpy(path, pszDir);
	if (path[_tcslen(path) - 1] != _T('\\'))
		_tcscat(path, _T("\\"));
	_tcscat(path, _T("*.*"));

	if( !finder.FindFile(path) )
	{
		return TRUE;
	}

    do
    {
        TCHAR	file[MAX_PATH];

		if( finder.IsDots() )
		{
			continue;
		}

		_tcscpy(file, pszDir);
		if (file[_tcslen(file) - 1] != _T('\\'))
			_tcscat(file, _T("\\"));
		_tcscat( file, finder.GetFileName() );

		DWORD attrib = GetFileAttributes(file);

        if( attrib&FILE_ATTRIBUTE_DIRECTORY )
        {
			if( !RemoveDirAll(file) )
			{
				return FALSE;
			}
			if( !RemoveDirectory(file) )
			{
				return FALSE;
			}
        }
        else
        {
			if( !DeleteFile(file) )
			{
				return FALSE; 
			}
        }
    }while ( finder.FindNextFile() );

	return TRUE;
}

int	 GetSubDirLevel(wchar_t* pathName)
{
	int level = 0;
	
	TCHAR		tmpPath[MAX_PATH];
	TCHAR		path[MAX_PATH];
	TCHAR		buf[MAX_PATH];
	CFindFile	finder;
	CSimpleArray<TPath> pathArray;

	_tcscpy(path, pathName);
	pathArray.Add(*(TPath*)path);

	for( ; ; )
	{
		//pop out 1 from stack
		int n = pathArray.GetSize();
		if( n<=0 )
		{
			break;
		}
		_tcscpy(path, pathArray[n-1].path);
		pathArray.RemoveAt(n-1);

		//find file
		_tcscpy(buf, path);
		if (buf[_tcslen(buf) - 1] != _T('\\'))
			_tcscat(buf, _T("\\"));

		_tcscpy(tmpPath, buf);
		_tcscat(buf, _T("*.*"));

		//get deep
		int tmpDeep = 0;
		const TCHAR* p = tmpPath + _tcslen(tmpPath);
		
		do
		{
			if(*p ==_T('\\'))
			{
				tmpDeep++;
			}
			p--;
		}while (p>=tmpPath);

		if(tmpDeep>level)
			level = tmpDeep;

		if( !finder.FindFile(buf) )
		{
			continue;
		}

		do
		{
			if( finder.IsDots() )
			{
				continue;
			}

			_tcscpy(buf, path);
			if (buf[_tcslen(buf) - 1] != _T('\\'))
				_tcscat(buf, _T("\\"));
			_tcscat(buf, finder.GetFileName());

			DWORD attrib = GetFileAttributes(buf);

			if( attrib & FILE_ATTRIBUTE_INROM )
			{
				continue;
			}
 
			if( !(attrib&FILE_ATTRIBUTE_DIRECTORY) )
			{
			}
			else
			{
				pathArray.Add( *(TPath*)buf );
			}

		}while ( finder.FindNextFile() );


		// find close
		finder.Close();
	}

	//get root deep
	_tcscpy(tmpPath, pathName);
	if (tmpPath[_tcslen(tmpPath) - 1] != _T('\\'))
				_tcscat(tmpPath, _T("\\"));
	int tmpDeep = 0;
	const TCHAR* p = tmpPath + _tcslen(tmpPath);
	do
	{
		if(*p ==_T('\\'))
		{
			tmpDeep++;
		}
		p--;
	}
	while(p>=tmpPath);

    return (level-tmpDeep);	
}

BOOL IsDir(const wchar_t* name)
{
	DWORD attribute;
	attribute = GetFileAttributes(name);

	if ((attribute & FILE_ATTRIBUTE_DIRECTORY) == 0) //file
    {
		return FALSE;
    }
	else
	{
		return TRUE;
	}
}

BOOL GetParentPath(const wchar_t* fullname, wchar_t* parentpath, wchar_t* name)
{	
	wchar_t scFullname[MAX_PATH];
	_tcscpy(scFullname,fullname);
	if(scFullname[_tcslen(scFullname)-1] == _T('\\'))
	{
		ZeroMemory(scFullname,MAX_PATH);
		_tcsncpy(scFullname,fullname,_tcslen(fullname)-1);
	}

	const wchar_t* tmpFileName = scFullname+_tcslen(scFullname);
	while (tmpFileName>scFullname && *tmpFileName!=_T('\\')) tmpFileName--;

	if((_tcslen(scFullname) == _tcslen(tmpFileName)) && (*tmpFileName ==_T('\\')))
	{
		_tcscpy(parentpath,_T("\\"));
		tmpFileName++;
		_tcscpy(name,tmpFileName);	

		return TRUE;
	}
	else if(_tcslen(scFullname) > _tcslen(tmpFileName))
	{
		tmpFileName++;
		_tcscpy(name,tmpFileName);
		int len = _tcslen(scFullname)-_tcslen(tmpFileName);
		_tcsncpy(parentpath,scFullname,len);

		ZeroMemory(scFullname,MAX_PATH);
		_tcscpy(scFullname,parentpath);

		if (scFullname[_tcslen(scFullname) - 1] != _T('\\'))
			_tcscat(scFullname, _T("\\"));

		_tcscpy(parentpath,scFullname);

		return TRUE;
	}

	return FALSE;
}

BOOL GetFileExt(const wchar_t* filename, wchar_t* fileext, wchar_t* name)
{
	DWORD attribute;
	wchar_t scFileName[MAX_PATH];
	_tcscpy(scFileName,filename);

	attribute = GetFileAttributes(scFileName);

	if ((attribute & FILE_ATTRIBUTE_DIRECTORY) == 0) //file
    {
		const wchar_t* ext = scFileName+_tcslen(scFileName);

		while (ext>scFileName && *ext!=_T('.')) ext--;

		if (ext==scFileName && *ext!=_T('.')) 
		{
			return FALSE;
		}
		
		_tcscpy(fileext,ext);
		if(name)
		{
			_tcsncpy(name,scFileName,_tcslen(scFileName)-_tcslen(ext));
		}
    }
	else
	{
		return FALSE;
	}
	
	return TRUE;
}

BOOL SepExtAndName(const wchar_t* filename, wchar_t* fileext, wchar_t* name)
{
	DWORD attribute;
	wchar_t scFileName[MAX_PATH];
	_tcscpy(scFileName,filename);

	{
		const wchar_t* ext = scFileName+_tcslen(scFileName);

		while (ext>scFileName && *ext!=_T('.')) ext--;

		if (ext==scFileName && *ext!=_T('.')) 
		{
			return FALSE;
		}

		_tcscpy(fileext,ext);
		if(name)
		{
			_tcsncpy(name,scFileName,_tcslen(scFileName)-_tcslen(ext));
		}
	}

	return TRUE;
}


